//
//  RecipientInfoView.m
//  WukongForwarder
//
//  Created by sf-airiOs on 2018/1/18.
//  Copyright © 2018年 滕睿(Ray Teng)-航空运力平台项目. All rights reserved.
//

#import "RecipientInfoView.h"
#import "UIImageView+WebCache.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

@implementation RecipientInfoView

//重写拉layout
- (void)layoutSubviews{
    [super layoutSubviews];
    //适配宽度
    for (UIView *sub in self.subviews) {
        [sub mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(SCREEN_WIDTH));
        }];
    }
    //设置滚动区域
    CGFloat sizeValue = (CGRectGetMaxY(self.bottomView.frame) > self.frame.size.height) ?CGRectGetMaxY(self.bottomView.frame):self.frame.size.height;
    self.contentSize = CGSizeMake(SCREEN_WIDTH, sizeValue + 1);
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    // 隐藏滚动条
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.accessoryBottom.constant = 20;
    self.repairFreight.attributedText = [self attributedStringShow:self.repairFreight.text];
}

//MARK: - --- 模型数据赋值展示
- (void)setInfoModel:(LBModel *)infoModel{
    _infoModel = infoModel;
    
    for (NSInteger i = 0; i < _infoModel.urls.count; i++) {
        UIImageView *accessory = [[UIImageView alloc] init];
        accessory.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.3];
        accessory.tag = i;
        accessory.contentMode = UIViewContentModeScaleAspectFill;
        accessory.layer.masksToBounds = YES;
        [self.secondView addSubview:accessory];
        [self.imageViews addObject:accessory];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewClick:)];
        accessory.userInteractionEnabled = YES;
        [accessory addGestureRecognizer:tap];
        [accessory mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.accessoryTt.mas_top).offset((50 + 10) *  (i / 3));
            make.left.equalTo(self.accessoryTt.mas_right).offset(10 + (50 + 10) * (i % 3));
            make.width.equalTo(@50);
            make.height.equalTo(@50);
        }];
        self.accessoryBottom.constant = (50 + 10) *  (i / 3) + 57;
        NSLog(@"index = %zi; x = %zi; y = %zi;",i,10 + (50 + 10) * (i % 3),(50 + 10) *  (i / 3));
        
        //计算相对于self的坐标
        //_infoModel.frames[i] = [NSValue valueWithCGRect:CGRectMake(45.5 + 10 + (50 + 10) * (i % 3), (50 + 10) *  (i / 3) + 297, 50, 50)];
        _infoModel.frames[i] = [NSValue valueWithCGRect:CGRectMake(CGRectGetMaxX(self.accessoryTt.frame) + 10 + (50 + 10) * (i % 3), (50 + 10) *  (i / 3) + CGRectGetMinY(self.secondView.frame) + CGRectGetMinY(self.accessoryTt.frame), 50, 50)];
        
    }
    
    [self showModel:_infoModel];
}

- (void)showModel:(LBModel *)model {

    __weak typeof (*& self) wself = self;
    [self.imageViews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imageView = (UIImageView *)obj;
        imageView.hidden = YES;
        imageView.image = nil;
    }];
    [model.frames enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imageV = wself.imageViews[idx];
        imageV.hidden = NO;
        //imageV.frame = [model.frames[idx] CGRectValue];
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.urls[idx].thumbnailURLString] placeholderImage:[UIImage imageNamed:@"LBLoadError.jpg"]];
        
    }];
}

//MARK: - --- 富文本
- (NSMutableAttributedString *)attributedStringShow:(NSString *)str{
    NSRange rag = [str rangeOfString:@"."];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:str];
    [attrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(rag.location, 3)];
    return attrString;
}

- (void)imageViewClick:(UITapGestureRecognizer *)tap {
    if (_callBack) {
        _callBack(self.infoModel,tap.view.tag);
    }
}

- (NSMutableArray *)imageViews {
    if (!_imageViews) {
        _imageViews = [[NSMutableArray alloc]init];
    }
    return _imageViews;
}

@end
